home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / utilitys / 93 / makersh / util.c < prev   
Encoding:
C/C++ Source or Header  |  1987-10-12  |  3.8 KB  |  187 lines

  1. /*
  2.    This program may be freely
  3.   distributed , copied,and modified so long
  4. as a fee is not charged. However,
  5. I reserve all rights available to
  6. me under The Copyright Act.
  7. If you find any bugs please feel free to contact me.
  8.   David DeGeorge
  9.   Princeton,New Jersey
  10.   March 1987
  11.  
  12. USENET:  ....princeton!idacrd!dld
  13. COMPUSERVE: 74176,3210
  14. GENIE: DEGEORGE
  15. */
  16.  
  17. /* This file
  18. some utility routines 
  19. for makersh
  20. N.B. form_alert and fsel_input are called by some of the below routines
  21. if you wish to make this a TOS application you should not use these
  22. e.g. get_files should be replaced .
  23. */
  24.  
  25. #include "makersh.h"
  26. #include <osbind.h>
  27. #include <aesbind.h>
  28. #ifdef GEM
  29. begin()
  30. {
  31.     open_work();
  32. }
  33. leave()
  34. {
  35.     close_work();
  36.     exit(1);
  37. }
  38. #endif
  39.  
  40.  
  41. long findstr(ptr)
  42. long ptr;
  43. {
  44.     int i;
  45.     for ( i = 0 ;i < nstrings ; i++){
  46.          if ( stradds[i] == ptr) return((long) i);
  47.         }
  48.         error("findstr error",FATAL);
  49. }        
  50. long findbb(ptr)
  51. long ptr;
  52. {
  53.     int i;
  54.     for ( i=0 ; i < rheader.rsh_nbb ; i++)
  55.         if( bbadds[i] == ptr) return((long) i);
  56.            error("findbb error",FATAL);
  57.         }
  58.  
  59.  
  60. #ifdef GEM
  61. getfiles(inaddr,outaddr)
  62. FILE **inaddr,**outaddr;
  63. {
  64.        char filename[80];
  65.        char mesag[80];
  66.        int but;
  67.        FILE *temp; 
  68.        do {
  69.             if(get_file("RSC",filename)==FALSE)leave();
  70.             
  71.     }    
  72.             while ( access(filename,CANREAD) != 0);
  73.             *inaddr = fopen(filename,"rb");
  74.           filename[0]='\0';
  75.          while(1){
  76.                  if(get_file("RSH",filename)==FALSE)leave();
  77.                  if ( access(filename,EXISTS) == 0){
  78.        but = form_alert(2,"[2][File exists      |Overwrite?      ][yes|no|cancel]");    
  79.                   if ( but == 2)continue;
  80.                   if(but == 3)leave();
  81.                 }
  82.                   if((temp = fopen(filename,"w"))== NULL){
  83.     sprintf(mesag,"[2][Can't open|%s|for a write    ][try again|abort]",filename);
  84.                   but = form_alert(2,mesag);
  85.                   if (but == 2) leave();
  86.                   continue;
  87.         }                  
  88.                 break;
  89.       }            
  90.        *outaddr = temp;
  91.  
  92. }          
  93.  
  94.  
  95.  
  96.  
  97.  
  98. /* The below four routines were glombed from
  99.    Time Oren's column on
  100.    Compuserve
  101.  */
  102.   
  103.  
  104. get_file(extnt, got_file)
  105.     char    *extnt, *got_file;
  106.     {
  107.     int    butn, ii;
  108.     char    tmp_path[64], tmp_name[13];
  109.     tmp_name[0] = '\0';
  110.     tmp_path[0] = '\0';
  111.  
  112.     if (*got_file)
  113.         parse_fname(got_file, tmp_path, tmp_name, extnt);
  114.     if (!tmp_path[0])
  115.         get_path(&tmp_path[0], extnt);
  116.  
  117.     fsel_input(tmp_path, tmp_name, &butn);
  118.     if (butn)
  119.         {
  120.         strcpy(got_file, tmp_path);
  121.         for (ii = 0; got_file[ii] && got_file[ii] != '*'; ii++);
  122.         got_file[ii - 1] = '\0';
  123.         strcat (got_file, "\\");
  124.         strcat(got_file, tmp_name);
  125.         return (TRUE);
  126.         }
  127.     else
  128.         return (FALSE);
  129.     }
  130.  
  131.  
  132. parse_fname(full, path, name, extnt)
  133.     char    *full, *path, *name, *extnt;
  134.     {
  135.     int    i, j;
  136.     char    *s, *d;
  137.  
  138.     for (i = strlen(full); i--; )        /* scan for end of path */
  139.         if (full[i] == '\\' || full[i] == ':')
  140.             break;
  141.     if (i == -1)
  142.         strcpy(name, full);        /* "Naked" file name */
  143.     else
  144.         {
  145.         strcpy(name, &full[i+1]);
  146.         for (s = full, d = path, j = 0; j++ < i + 1;
  147.             *d++ = *s++);
  148.         strcpy(&path[i+1], "*.");
  149.         strcat(path, extnt);
  150.         }
  151.     }
  152.  
  153.  
  154.  
  155. get_path(tmp_path, spec)
  156.     char    *tmp_path, *spec;
  157.     {
  158.     int    cur_drv;
  159.  
  160.     cur_drv = Dgetdrv();
  161.     tmp_path[0] = cur_drv + 'A';
  162.     tmp_path[1] = ':';
  163.     Dgetpath(&tmp_path[2], 0);
  164.     if (strlen(tmp_path) > 3)
  165.         strcat(tmp_path, "\\");
  166.     else
  167.         tmp_path[2] = '\0';
  168.     strcat(tmp_path, "*.");
  169.     strcat(tmp_path, spec);
  170.     }
  171.  
  172.  
  173.  
  174. void new_ext(o_fname, n_fname, ext)
  175.     char    *o_fname, *n_fname, *ext;
  176.     {
  177.     int    ii, jj;
  178.  
  179.     strcpy(n_fname, o_fname);
  180.     for (ii = (jj = strlen(n_fname)) - 1; ii && n_fname[ii] != '.'; ii--);
  181.     if (!ii)
  182.         n_fname[ii = jj] = '.';
  183.     strcpy(&n_fname[++ii], ext);
  184.     }
  185. #endif
  186.  
  187.